﻿Imports DNP.GestionComercial.Util
Imports System.Windows.Forms
Public Class frmFormBase
    Public WithEvents pMenuMantenimiento As System.Windows.Forms.ToolStrip
#Region "declaracion de variables"
    Public _blnClickNuevo As Boolean = False
    Public blnValidoNuevo As Boolean = False
    Public _blnClickGrabar As Boolean = False
    Public _blnSalir As Boolean = False


#End Region

    Public Sub New()

        ' Llamada necesaria para el diseñador.
        InitializeComponent()

        Me.tbcMantenimiento.TabPages.Remove(Me.tbpSubDetalle)


        ' Agregue cualquier inicialización después de la llamada a InitializeComponent().


    End Sub



    Protected Overridable Sub tsbNuevo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbNuevo.Click

    End Sub

    Protected Overridable Sub tsbAbrir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbAbrir.Click

    End Sub
    Protected Overridable Sub tsbGuardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbGuardar.Click

    End Sub
    Protected Overridable Sub tsbSalir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbSalir.Click

    End Sub
    Protected Overridable Sub tsbEliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbEliminar.Click

    End Sub
    Protected Overridable Sub tsbListar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbListar.Click

    End Sub
    Protected Overridable Sub tsbImprimir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbImprimir.Click

    End Sub

    Protected Overridable Sub tsbPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbPreview.Click

    End Sub

    Protected Overridable Sub tsbHelp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbHelp.Click

    End Sub

    Protected Overridable Sub tsbExportarExcel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbExportarExcel.Click

    End Sub

    Protected Sub ValidaSalirApp()
        If _blnClickNuevo Then
            Dim oMsg As New sisMensaje

        End If
    End Sub
    '{
    '    if (_blnClickNuevo) 
    '    {
    '        Mensajes oMsg = new Mensajes();
    '        if (oMsg.showMsgQuestion("msgCerrarAplicacion") == DialogResult.Yes)
    '        {
    '            _blnSalir = true;
    '            Dispose();                    
    '        }
    '    }
    '    else 
    '    {
    '        _blnSalir = true;
    '        Dispose();                
    '    }
    '}
#Region "eventos"
    Public Sub asignarEventos(ByVal pControl As Control)
        Dim nombre As String = ""
        If TypeOf pControl Is TextBox Then
            nombre = pControl.Name.ToUpper
            If pControl.Name.StartsWith("TXTINT_") Then
                AddHandler pControl.KeyPress, AddressOf SoloNumerosPositivos
            End If

            If pControl.Name.StartsWith("TXTDEC_") Then
                AddHandler pControl.KeyPress, AddressOf SoloDecimales
            End If

        End If
        If pControl.HasChildren Then
            For Each vcontrol As Control In pControl.Controls
                asignarEventos(vcontrol)
            Next
        End If

    End Sub
    Public Sub SoloNumerosPositivos(ByVal sender As System.Object, _
                ByVal e As System.Windows.Forms.KeyPressEventArgs)

        Select Case Asc(e.KeyChar)
            Case AscW(ControlChars.Cr) 'Enter key
                e.Handled = True

            Case AscW(ControlChars.Back) 'Backspace

            Case 48 To 57 'Negative sign, Decimal and Numbers

            Case Else ' Everything else
                e.Handled = True
        End Select
    End Sub

    Public Sub SoloDecimales(ByVal sender As System.Object, _
               ByVal e As System.Windows.Forms.KeyPressEventArgs)

        Select Case Asc(e.KeyChar)
            Case AscW(ControlChars.Cr) 'Enter key
                e.Handled = True

            Case AscW(ControlChars.Back) 'Backspace

            Case 46, 48 To 57 'Negative sign, Decimal and Numbers

            Case Else ' Everything else
                e.Handled = True
        End Select
    End Sub

    Public Sub HabilitaSoloNumeros(ByVal e As KeyPressEventArgs)
        Dim tecla As Integer = Asc(e.KeyChar)
        If tecla < 48 Or tecla > 57 Then
            e.Handled = True
        End If
    End Sub


    Public Sub HabilitaSoloDecimales(ByVal e As KeyPressEventArgs)
        Dim tecla As Integer = Asc(e.KeyChar)
        If (tecla = 46 Or tecla = 8 Or (tecla > 47 And tecla < 58)) Then
            e.Handled = False
        Else
            e.Handled = True
        End If
    End Sub

#End Region




    Public Sub limpiarControlesForm(ByVal pControl As Control)
        Dim nombre As String = ""
        If TypeOf pControl Is TextBox Then
            pControl.Text = ""
        End If

        If (pControl.GetType() Is GetType(ComboBox)) Then
            Dim cbobx As ComboBox = CType(pControl, ComboBox)
            If cbobx.Items.Count = 0 Then
                cbobx.Items.Add("[Seleccionar]")
                cbobx.SelectedIndex = 0

            Else
                cbobx.SelectedIndex = 0
            End If

        End If

        If pControl.HasChildren Then
            For Each vcontrol As Control In pControl.Controls
                limpiarControlesForm(vcontrol)
            Next
        End If

    End Sub



    Protected Overridable Sub grdLista_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles grdLista.Click

    End Sub


#Region "Funciones de Errores"
    Public objListaError As New TError
    'Property objListaError As TError
    '    Get
    '        Return _lstError
    '    End Get
    '    Set(ByVal value As TError)
    '        _lstError = value
    '    End Set
    'End Property


    Public Function ValidarStringVacio(ByVal strValue As String, ByVal strFieldName As String, ByVal blnIsRequired As Boolean) As String

        validateField(strValue, strFieldName, blnIsRequired)
        If (strValue.Length > 0) Then
            Return strValue
        Else
            Return ""
        End If

    End Function
    Public Function ValidarComboVacio(ByVal strValue As String, ByVal strFieldName As String, ByVal blnIsRequired As Boolean, ByVal pValorInicial As String) As Boolean
        If blnIsRequired Then
            If strValue Is Nothing Or strValue = pValorInicial Then
                objListaError.strCampoError = strFieldName
                objListaError.intNumError += 1
                Return False
            End If

        End If
        Return True
    End Function

    Private Sub validateField(ByRef strvalue As String, ByVal strFieldName As String, ByVal blnIsRequired As Boolean)
        '  Dim vError As New TError
        If strvalue Is Nothing Then
            objListaError.strCampoError = strFieldName
            objListaError.intNumError += 1
            ' objListaError.Add(vError)
            strvalue = ""
        Else

            strvalue = strvalue.Trim()
            If (blnIsRequired) Then
                If strvalue.Length = 0 Then
                    objListaError.strCampoError = strFieldName
                    objListaError.intNumError += 1

                End If
            End If
        End If



    End Sub


    Public Function ValidarIntVacio(ByVal strValue As String, ByVal strFieldName As String, ByVal blnIsRequired As Boolean)
        validateField(strValue, strFieldName, blnIsRequired)
        Dim intValue As Integer
        If (strValue.Length > 0 And strValue <> "0") Then
            Try

                intValue = System.Int32.Parse(strValue, System.Globalization.NumberStyles.Any)

            Catch ex As Exception
                intValue = 0
            End Try
        Else
            intValue = 0

        End If

        If blnIsRequired Then
            If intValue = 0 Then
                objListaError.strCampoError = strFieldName
                objListaError.intNumError += 1
            Else
                intValue = 1
            End If
        Else
            intValue = 1
        End If
        Return intValue
    End Function

    Public Function ValidarDecimalVacio(ByVal strValue As String, ByVal strFieldName As String, ByVal blnIsRequired As Boolean)
        validateField(strValue, strFieldName, blnIsRequired)
        Dim intValue As Integer
        If (strValue.Length > 0 And strValue <> "0") Then
            Try

                intValue = System.Double.Parse(strValue, System.Globalization.NumberStyles.Any)

            Catch ex As Exception
                intValue = 0
            End Try
        Else
            intValue = 0

        End If
        If blnIsRequired Then
            If intValue = 0 Then
                objListaError.strCampoError = strFieldName
                objListaError.intNumError += 1
            Else
                intValue = 1
            End If
        Else
            intValue = 1
        End If
        Return intValue

    End Function

    Public Sub showError()
        showError(True)
    End Sub



    Private Sub showerror(ByVal blnShowAlert As Boolean)

        Dim strResourceFile As String = "DNP.GestionComercial.FormBase.Resources"
        Dim rm As Resources.ResourceManager = New Resources.ResourceManager(strResourceFile, System.Reflection.Assembly.GetExecutingAssembly)
        Dim strTitle As String = rm.GetString("msgTituloMensaje")
        Dim strMsgShowError As String = rm.GetString("msgShowError")
        Dim strMsgClass As String = objListaError.strCampoError + " " + strMsgShowError
        strMsgClass = strMsgClass.Replace("(*)", "").Trim()
        MessageBox.Show(strMsgClass, strTitle, MessageBoxButtons.OK, MessageBoxIcon.Information)

    End Sub



    Protected Function ValidaNumCaracter(ByVal intNumCaracter As Integer, ByVal strValue As String) As String
        If (strValue.Trim().Length <> intNumCaracter) Then
            strValue = ""
        End If

        Return strValue
    End Function






#End Region





    Protected Overridable Sub grdLista_DataBindingComplete(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewBindingCompleteEventArgs) Handles grdLista.DataBindingComplete

    End Sub

    Protected Overridable Sub txtEntidad_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtEntidad.KeyPress

    End Sub

    Private Sub frmFormBase_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim myFont As New Font("Arial", 8)

        ChangeFontName(Me, myFont)
    End Sub


    Public Shared Sub ChangeFontName(ByVal aForm As Form, ByVal aFont As Font)

        For Each frm As Form In My.Application.OpenForms
            If frm.HasChildren = True Then
                For Each ctrl As Control In frm.Controls
                    ChangeFontName(ctrl, aFont)
                Next
            End If
        Next

        If aForm.IsMdiContainer = True Then
            For Each frm As Form In aForm.MdiChildren
                If frm.HasChildren = True Then
                    For Each ctrl As Control In frm.Controls
                        ChangeFontName(ctrl, aFont)
                    Next
                End If
            Next
        End If

    End Sub

    Public Shared Sub ChangeFontName(ByVal aControl As Control, ByVal aFont As Font)

        If aControl.HasChildren = True Then
            For Each ctrl As Control In aControl.Controls
                ctrl.Font = New Font(aFont.Name, ctrl.Font.Size, ctrl.Font.Style, ctrl.Font.Unit, ctrl.Font.GdiCharSet, ctrl.Font.GdiVerticalFont)
                ChangeFontName(ctrl, aFont)
            Next
        Else
            'This line added as child controls without children were not being changed.>>
            aControl.Font = New Font(aFont.Name, aControl.Font.Size, aControl.Font.Style, aControl.Font.Unit, aControl.Font.GdiCharSet, aControl.Font.GdiVerticalFont)
        End If

    End Sub

End Class


Public Class TError
   
    Private _intNumError As Integer = 0
    Property intNumError As Integer
        Get
            Return _intNumError
        End Get
        Set(ByVal value As Integer)
            _intNumError = value
        End Set
    End Property

    Private _strCampoError As String
    Property strCampoError As String
        Get
            Return _strCampoError
        End Get
        Set(ByVal value As String)
            _strCampoError = value
        End Set
    End Property
End Class